#!C:/strawberry/perl/bin/perl.exe

use strict;
use warnings;

use CGI;
use RPC::XML;
use RPC::XML::Client;
use Data::Dumper;
use XML::Simple;
use XML::eXistDB::RPC;
use XML::Compile::RPC::Client;



# read the CGI params
my $cgi = CGI->new;
my $nombre = $cgi->param("nombre");
my $apellidos = $cgi->param("apellidos");
my $correo = $cgi->param("correo");
my $password = $cgi->param("password");
#my $sexo = $cgi->param("sexo");


my $eXist = XML::eXistDB::RPC->new( destination=>"http://localhost:8080/exist/xmlrpc", repository=>"/db", user=>"admin", password=>"eaf2julian" ) ;



my $xquery = <<END;
    for \$puntero in doc("/placeanddate/usuarios.xml")/usuarios
    return
	update insert 
            <usuario>
                <nombre>$nombre</nombre>
                <apellidos>$apellidos</apellidos>
                <password>$password</password>
                <email>$correo</email>
            </usuario>
        into doc("/placeanddate/usuarios.xml")/usuarios
END


my ($rc1, $set) = $eXist->executeQuery($xquery) ;
#my ($rc2, $count) = $eXist->numberOfResults($set) ;
#my ($rc3, @data) = $eXist->retrieveResults($set) ;
#$eXist->releaseResultSet($set) ;


# create a JSON string according to the database result
my $json =qq{{"success" : "login is successful", "userid" : "ko"}};
# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;